package com.digitalchina.lxnsy.base;

import android.content.Context;
import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.base
 * 创建人：郑皓
 * 创建时间：2019/4/23 10:48
 * 修改备注：
 * ================================================
 */
public abstract class BaseListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.et_search)
    public EditText etSearch;
    @Bind(R.id.iv_cancel)
    public ImageView ivCancel;
    @Bind(R.id.layout_search)
    public RelativeLayout layoutSearch;
    @Bind(R.id.recycler_view)
    public EasyRecyclerView recyclerView;

    public RecyclerArrayAdapter adapter;
    public int page = 1;
    public int limit = 10;

    public String keyword = "";



    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_base_list);
    }

    @Override
    public void initMember() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        setAdapter();
    }


    /**
     * 分割线
     * @param style 0-不设置  1-1px分隔线  2-10px分隔条
     */
    protected void setDecoration(int style){

        switch (style){
            case 0:
                // nothing
                break;
            case 1:
                recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 1, 0, 0));
                break;
            case 2:
                recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 10, 0, 0));
                break;
        }
    }

    protected abstract void setAdapter();

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etSearch.setText("");
                keyword = "";
                clearSearch();
            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) textView.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(textView.getApplicationWindowToken(), 0);
                    }
                    // 刷新列表数据
                    if (!keyword.equals(etSearch.getText().toString())) {
                        keyword = etSearch.getText().toString();
                        search();
                    }
                    return true;
                }
                return false;
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                onClick(position);
            }
        });

    }

    protected void onClick(int position) {

    }

    protected void search() {
    }

    protected void clearSearch() {
    }

    protected void showSearch() {
        layoutSearch.setVisibility(View.VISIBLE);
    }


    @Override
    public abstract void setHandler();

    @Override
    public abstract void callBack(Object object);

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public abstract void onRefresh();

    @Override
    public abstract void onLoadMore();
}
